package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.po.Account;
import com.itheima.service.AccountService;
import com.itheima.utils.TransactionManager;

import java.util.List;

/**
 * ClassName:AccountServiceImpl
 * Package:com.itheima.service.impl
 * Description:账户的业务层实现类
 * <p>
 * 事务控制应该全都在业务层
 * Date:2020/2/21 15:14
 * User: snow
 */

public class AccountServiceImpl implements AccountService {

	private AccountDao accountDao;
	private TransactionManager transactionManager;

	public void setTransactionManager(TransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

	public void setAccountDao(AccountDao accountDao) {

		this.accountDao = accountDao;
	}

	public List<Account> findAllAccount() {
		try {
			// 1、开启事务
			transactionManager.beginTransaction();
			// 2、执行操作
			List<Account> accounts = accountDao.findAllAccount();
			// 3、提交事务
			transactionManager.commit();

			return accounts;
		} catch (Exception e) {
			// 5、回滚操作
			transactionManager.rollback();
			throw new RuntimeException(e);
		} finally {
			// 6、释放连接
			transactionManager.release();
		}

	}

	public Account findAccountById(Integer id) {
		try {
			// 1、开启事务
			transactionManager.beginTransaction();
			// 2、执行操作
			Account account = accountDao.findAccountById(id);
			// 3、提交事务
			transactionManager.commit();
			return account;
		} catch (Exception e) {
			// 5、回滚操作
			transactionManager.rollback();
			throw new RuntimeException(e);
		} finally {
			// 6、释放连接
			transactionManager.release();
		}



	}

	public Account findAccountByName(String accountName) {

		try {
			// 1、开启事务
			transactionManager.beginTransaction();
			// 2、执行操作
			Account account = accountDao.findAccountByName(accountName);
			// 3、提交事务
			transactionManager.commit();
			//返回结果
			return account;

		} catch (Exception e) {
			// 5、回滚操作
			transactionManager.rollback();
			throw new RuntimeException(e);
		} finally {
			// 6、释放连接
			transactionManager.release();
		}



	}

	public void saveAccount(Account account) {

		try {
			// 1、开启事务
			transactionManager.beginTransaction();
			// 2、执行操作

			accountDao.saveAccount(account);
			// 3、提交事务
			transactionManager.commit();

		} catch (Exception e) {
			// 5、回滚操作
			transactionManager.rollback();
			throw new RuntimeException(e);
		} finally {
			// 6、释放连接
			transactionManager.release();
		}

	}

	public void updateAccount(Account account) {

		try {
			// 1、开启事务
			transactionManager.beginTransaction();
			// 2、执行操作
			accountDao.updateAccount(account);
			// 3、提交事务
			transactionManager.commit();

		} catch (Exception e) {
			// 5、回滚操作
			transactionManager.rollback();
			throw new RuntimeException(e);
		} finally {
			// 6、释放连接
			transactionManager.release();
		}

	}

	public void deleteAccount(Integer id) {

		try {
			// 1、开启事务
			transactionManager.beginTransaction();
			// 2、执行操作
			accountDao.deleteAccount(id);
			// 3、提交事务
			transactionManager.commit();

		} catch (Exception e) {
			// 5、回滚操作
			transactionManager.rollback();
			throw new RuntimeException(e);
		} finally {
			// 6、释放连接
			transactionManager.release();
		}

	}

	public void tansfer(String sourceName, String targetName, Float money) {
		try {
			// 1、开启事务
			transactionManager.beginTransaction();
			// 2、执行操作
			// 根据名称查询转出账户
			Account source = accountDao.findAccountByName(sourceName);
			// 根据名称查询转入账户
			Account target = accountDao.findAccountByName(targetName);
			// 转出账户数据操作
			source.setMoney(source.getMoney() - money);
			// 转入账户数据操作
			target.setMoney(target.getMoney() + money);
			// 更新转出账户
			accountDao.updateAccount(source);
			// int i = 1 / 0;
			// 更新转入账户
			accountDao.updateAccount(target);
			// 3、提交事务
			transactionManager.commit();

		} catch (Exception e) {
			// 5、回滚操作
			transactionManager.rollback();
			throw new RuntimeException(e);
		} finally {
			// 6、释放连接
			transactionManager.release();
		}
	}
}
